package com.devenv.entity;

import com.devenv.entity.base.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.math.BigDecimal;
import java.util.List;

/**
 * 软件信息实体类
 * 继承BaseEntity获得审计功能
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "software", indexes = {
    @Index(name = "idx_software_category", columnList = "category"),
    @Index(name = "idx_software_name", columnList = "name"),
    @Index(name = "idx_software_deleted", columnList = "deleted")
})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SQLDelete(sql = "UPDATE software SET deleted = true WHERE id = ?")
@Where(clause = "deleted = false")
public class Software extends BaseEntity {

    @NotBlank(message = "软件名称不能为空")
    @Size(max = 100, message = "软件名称长度不能超过100字符")
    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @NotBlank(message = "软件分类不能为空")
    @Size(max = 50, message = "分类长度不能超过50字符")
    @Column(name = "category", nullable = false, length = 50)
    private String category;

    @Size(max = 2000, message = "描述长度不能超过2000字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Size(max = 50, message = "版本长度不能超过50字符")
    @Column(name = "software_version")
    private String softwareVersion;

    @Size(max = 20, message = "大小信息长度不能超过20字符")
    @Column(name = "size", length = 20)
    private String size;

    @Size(max = 500, message = "下载地址长度不能超过500字符")
    @Column(name = "download_url", length = 500)
    private String downloadUrl;

    @Size(max = 500, message = "主页地址长度不能超过500字符")
    @Column(name = "homepage", length = 500)
    private String homepage;

    /**
     * 软件标签（JSON格式存储）
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "tags")
    private List<String> tags;

    @Size(max = 200, message = "图标路径长度不能超过200字符")
    @Column(name = "icon", length = 200)
    private String icon;

    /**
     * 支持的操作系统（JSON格式存储）
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "os")
    private List<String> os;

    @Size(max = 100, message = "许可证长度不能超过100字符")
    @Column(name = "license", length = 100)
    private String license;

    /**
     * 下载次数
     */
    @Column(name = "download_count")
    private Long downloadCount = 0L;

    /**
     * 查看次数
     */
    @Column(name = "view_count")
    private Long viewCount = 0L;

    /**
     * 平均评分
     */
    @Column(name = "rating", precision = 3, scale = 2)
    private BigDecimal rating = new BigDecimal("0.00");

    /**
     * 是否为推荐软件
     */
    @Column(name = "featured")
    private Boolean featured = false;

    /**
     * 软件优先级（用于排序）
     */
    @Column(name = "priority")
    private Integer priority = 0;

    /**
     * 安装命令（JSON格式存储）
     */
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "install_commands")
    private Object installCommands;

    /**
     * 软件状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private SoftwareStatus status = SoftwareStatus.ACTIVE;

    /**
     * 软件状态枚举
     */
    public enum SoftwareStatus {
        ACTIVE("启用"),
        INACTIVE("禁用"),
        DEPRECATED("已弃用");

        private final String description;

        SoftwareStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
